<template>
    <div class="reportmian">
        <el-collapse v-model="activeNames">
            <el-collapse-item title="数据筛选" name="1">
                <el-form :inline="true" class="divstyle" :model="selectfrom">
                    <el-form-item label="部门">
                        <el-select filterable placeholder="请选择部门" v-model="selectfrom.applyPurchase.Department">
                            <el-option :value="0" label="全部"></el-option>
                            <el-option v-for="v in AllDepartment" :label="v.DepartmentName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="项目">
                        <el-select filterable placeholder="请选择项目" v-model="selectfrom.applyPurchase.Project">
                            <el-option :value="0" label="全部"></el-option>
                            <el-option v-for="v in AllProject" :label="v.ProjectName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-date-picker v-model="selectfrom.Start"
                                        type="date"
                                        placeholder="开始时间"
                                        value-format="yyyy-MM-dd"
                                        format="yyyy - MM - dd">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item>
                        <el-date-picker v-model="selectfrom.End"
                                        type="date"
                                        placeholder="结束时间"
                                        value-format="yyyy-MM-dd"
                                        format="yyyy - MM - dd">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="success" icon="el-icon-search" @click="formdata"></el-button>
                        <el-button type="success" icon="el-icon-printer" v-print="printObj">打印</el-button>
                    </el-form-item>
                </el-form>
            </el-collapse-item>
            <el-collapse-item title="数据面板" name="2">
                <el-row>
                    <el-col align="center" style="font-size: 25px;font-weight: bold;">固远晨通科技发展有限公司申购单汇总表</el-col>
                    <el-col>
                        <el-table id="wrapperApplyPurchaseReport" :data="tableData" border show-summary>
                            <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
                            <el-table-column label="申购单号" align="center" width="120">
                                <template slot-scope="slot">
                                    {{ slot.row.ApplyPurchaseNumber }}
                                </template>
                            </el-table-column>
                            <el-table-column label="日期" align="center" width="120">
                                <template slot-scope="slot">
                                    {{ fmtDateNew(slot.row.ApplyDate) }}
                                </template>
                            </el-table-column>
                            <el-table-column label="申请人" align="center" prop="UserName" width="120"></el-table-column>
                            <el-table-column label="部门" align="center" width="150">
                                <template slot-scope="slot">
                                    {{ slot.row.DepartmentName }}
                                </template>
                            </el-table-column>
                            <el-table-column label="项目" align="center">
                                <template slot-scope="slot">
                                    {{ slot.row.ProjectName }}
                                </template>
                            </el-table-column>
                            <el-table-column label="用途" align="center">
                                <template slot-scope="slot">
                                    {{ conversion(slot.row.ApplyPurchaseCause) }}
                                </template>
                            </el-table-column>
                            <el-table-column label="金额（元）" align="center" prop="TotalPrice" width="150"></el-table-column>
                        </el-table>
                    </el-col>
                </el-row>
            </el-collapse-item>
            <el-collapse-item title="数据报表" name="3">
                <div id="EchartData">
                    <div class="pie">
                        <div class="pieData" id="leftData"></div>
                        <div class="pieData" id="rightData"></div>
                    </div>
                    <div class="bar" id="downData"></div>
                </div>
            </el-collapse-item>
        </el-collapse>
    </div>
</template>

<script>
    import * as publicjs from "../../../../public/Scriput/Public";
    import Echatrs from "echarts";

    export default {
        name: "ApplyPurchaseReport",
        props: {
            AllProject: {
                type: Array,
                required: true
            },
            AllDepartment: {
                type: Array,
                required: true
            }
        },
        data() {
            return {
                activeNames: ['1', '2', '3'],
                selectfrom: {
                    Start: '',
                    End: '',
                    applyPurchase: {
                        Department: 0,
                        Project: 0,
                    }
                },
                tableData: [],
                wrapper: {
                    width: "100%",
                    height: "100%",
                    margin: "auto",
                },
                printObj: {
                    id: "wrapperApplyPurchaseReport",  //打印标签的id
                    popTitle: '固远晨通科技发展有限公司申购单汇总表',  //文件标题
                    extraCss: 'http://www.guyuanct.com/',
                    extraHead: '<meta http-equiv="Content-Language"content="zh-cn"/>',
                },
            }
        },
        created() {
            this.InitDate();
            this.formdata()
            publicjs.setecharts(Echatrs)
        },
        methods: {
            InitDate() {
                const endDate = this.getFormatDate(new Date()).substr(0, 11)
                const beginDate = this.getFormatDate(new Date(new Date() - 3600 * 1000 * 24 * 30)).substr(0, 11)
                this.selectfrom.Start = beginDate;
                this.selectfrom.End = endDate;
            },
            // 获取当前时间
            getFormatDate(date) {
                var month = date.getMonth() + 1
                var strDate = date.getDate()
                if (month >= 1 && month <= 9) {
                    month = '0' + month
                }
                if (strDate >= 0 && strDate <= 9) {
                    strDate = '0' + strDate
                }
                var currentDate = date.getFullYear() + '-' + month + '-' + strDate + '' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()
                return currentDate
            },
            // 初始数据加载
            formdata() {
                let loadding = this.openLoading();
                this.$http.post('Report/GetDetialApplyPurchaseReportData', {
                    applyPurchase: this.selectfrom.applyPurchase,
                    Start: this.selectfrom.Start,
                    End: this.selectfrom.End,
                }).then(res => {
                    loadding.close();
                    this.tableData = res.data
                    this.InitEchart(this.tableData)
                })
            },
            //时间转化
            fmtDateNew(val) {
                if (val != '' && val != null) {
                    let newtime = val.split(' ')[0]
                    return newtime
                } else {
                    return ''
                }
            },
            //获取对应部门名字
            GetDepartmentName(value) {
                let name = ''
                if (value != 0) {
                    for (let i = 0; i < this.AllDepartment.length; i++) {
                        if (this.AllDepartment[i].Id == value) {
                            name = this.AllDepartment[i].DepartmentName
                            return name
                        }
                    }
                }
            },
            //获取对应项目名字
            GetProjectName(value) {
                let name = ''
                if (value != 0) {
                    for (let i = 0; i < this.AllProject.length; i++) {
                        if (this.AllProject[i].Id == value) {
                            name = this.AllProject[i].ProjectName
                            return name
                        }
                    }
                }
            },
            //获取人员名称
            GetUserName(value) {
                let name = ''
                if (value != 0) {
                    for (let i = 0; i < this.$store.state.AllUsers.length; i++) {
                        if (this.$store.state.AllUsers[i].Id == value) {
                            name = this.$store.state.AllUsers[i].UserName
                            return name
                        }
                    }
                }
            },
            //金额转化
            conversion(val) {
                if (val == '' || val == null) {
                    return ''
                } else {
                    return val
                }
            },
            // echarts表格加载
            InitEchart(data) {
                //按照部门分组统计
                var noRepeatDepartData = publicjs.DataGroupBy(JSON.stringify(data), "DepartmentId", "TotalPrice");
                var new_DepartName = noRepeatDepartData.map(obj => { return obj.DepartmentName; });
                var new_DepartData = noRepeatDepartData.map(obj => { return { name: obj.DepartmentName, value: obj.TotalPrice }; });
                publicjs.InitEchartPie("leftData", "部门金额占比", "单位：人民币（元）", new_DepartName, "部门金额占比", new_DepartData);
                //按照项目分组统计
                var noRepeatProjectData = publicjs.DataGroupBy(JSON.stringify(data), "ProjectId", "TotalPrice");
                var new_ProjectName = noRepeatProjectData.map(obj => { return obj.ProjectName == "" ? "其他" : obj.ProjectName; });
                var new_ProjectData = noRepeatProjectData.map(obj => { return { name: obj.ProjectName == "" ? "其他" : obj.ProjectName, value: obj.TotalPrice }; });
                publicjs.InitEchartPie("rightData", "项目金额占比", "单位：人民币（元）", new_ProjectName, "项目金额占比", new_ProjectData);
                //按照日期统计
                var noRepeatData = publicjs.DataGroupBy(JSON.stringify(data), "ApplyDate", "TotalPrice");
                var new_Name = noRepeatData.map(obj => { return this.fmtDateNew(obj.ApplyDate); });
                var new_Data = noRepeatData.map(obj => { return obj.TotalPrice; });
                publicjs.InitEchartBar("downData", "数据走势图", new_Name, new_Data, "金额（元）");
            },
        }
    }
</script>

<style scoped>
    #EchartData .pieData {
        float: left;
        width: 50%;
        height: 500px;
    }

    #EchartData .bar {
        width: 100%;
        height: 500px;
    }

    #EchartData .pie {
        height: 500px;
    }
</style>
